package com.kingsalert.kingsalert.view.process_widget;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.base.BaseProcessWidgetManager;
import com.kingsalert.kingsalert.bean.process.ProcessInfo_Data;
import com.kingsalert.kingsalert.dialog.DatePickerDialog;
import com.kingsalert.kingsalert.util.http.HttpUtil;


public class DateSelectViewManager extends BaseProcessWidgetManager {

    private TextView titleTv;
    private TextView inputTv;

    public DateSelectViewManager(View view, ProcessInfo_Data data,String page){
        super(view,data,page);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void init(){

        titleTv = mView.findViewById(R.id.vpds_tv_title);
        inputTv = mView.findViewById(R.id.vpds_tv_input);

        setTitle(mData.getTitle());
        inputTv.setHint(mData.getHint());

        mView.findViewById(R.id.vpds_view_click).setOnClickListener(v -> {

            HttpUtil.getInstance().saveBuriedPoint(page,mData.getParamName().toUpperCase()+"_CLICK","");

            new DatePickerDialog(
                    mView.getContext(),
                    mData.getTitle(),
                    date -> {
                        inputTv.setText(date);
                        HttpUtil.getInstance().saveBuriedPoint(page,mData.getParamName().toUpperCase()+"_CLICK",getInput());
                    }
            ).show();
        });

    }

    @Override
    public boolean checkInput() {
        if (!mData.isMustInput()) return true;
        return !TextUtils.isEmpty(inputTv.getText());
    }

    @Override
    public String getInput() {
        return inputTv.getText().toString();
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv.setVisibility(View.GONE);
        else {
            titleTv.setVisibility(View.VISIBLE);
            titleTv.setText(title);
        }
    }

}
